package org.dslintegration.merge;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collections;

import org.dslintegration.KM3StandaloneSetup;
import org.dslintegration.MRulesStandaloneSetup;
import org.dslintegration.kM3.KM3Factory;
import org.dslintegration.kM3.Metamodel;
import org.dslintegration.kM3.Package;
import org.dslintegration.kM3.impl.KM3FactoryImpl;
import org.dslintegration.mRules.MergeRules;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.xtext.resource.XtextResourceSet;

import com.google.inject.Injector;

public class InstanceLoader {
	public static Metamodel loadMetamodel(File file) throws IOException {
		Injector injector = new KM3StandaloneSetup().createInjectorAndDoEMFRegistration();
		return (Metamodel) loadInstance(injector, file).get(0);
	}

	public static MergeRules loadRules(File file) throws IOException {
		Injector injector = new MRulesStandaloneSetup().createInjectorAndDoEMFRegistration();
		return (MergeRules) loadInstance(injector, file).get(0);
	}

	public static Metamodel loadMetamodel(InputStream stream) throws IOException {
		Injector injector = new KM3StandaloneSetup().createInjectorAndDoEMFRegistration();
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		Resource resource = resourceSet.createResource(URI.createURI("instance:/loaded.km3"));

		// Load the resource
		resource.load(stream, resourceSet.getLoadOptions());
		return (Metamodel) resource.getContents().get(0);
	}

	protected static EList<EObject> loadInstance(Injector injector, File file) throws IOException {
		XtextResourceSet resourceSet = injector.getInstance(XtextResourceSet.class);
		Resource resource = resourceSet.createResource(URI.createURI("instance:/" + file.getAbsolutePath()));
		InputStream in = new FileInputStream(file);
		// Load the resource
		resource.load(in, resourceSet.getLoadOptions());
		return resource.getContents();
	}

	public static void main(String args[]) throws Exception {
		Metamodel mml = InstanceLoader.loadMetamodel(new File("C:/Projects/Master/org.dslintegration.MetaModelMerge/src/org/dslintegration/merge/inputB.km3"));
		MergeRules rules = InstanceLoader.loadRules(new File("C:/Projects/Master/org.dslintegration.MetaModelMerge/src/org/dslintegration/merge/Rules.mrs"));
		Metamodel mml2 = InstanceLoader.loadMetamodel(new File("C:/Projects/Master/org.dslintegration.MetaModelMerge/src/org/dslintegration/merge/inputA.km3"));

		KM3Factory factory = new KM3FactoryImpl();
		Package pack = factory.createPackage();
		pack.setName("PrimitiveTypes");

		mml.getPackages().add(pack);

		File fl = new File("c:/temp/generated.km3");
		fl.createNewFile();
		mml2.eResource().save(new FileOutputStream(fl), Collections.EMPTY_MAP);
	}
}
